# Maintainer: Johannes Löthberg <johannes@kyriasis.com>

pkgname=prometheus-node-exporter
pkgver=1.7.0
pkgrel=1

pkgdesc='Exporter for machine metrics'
url='https://github.com/prometheus/node_exporter'
arch=('loong64' 'x86_64')
license=('Apache')

depends=('glibc')
makedepends=('go' 'git')
options=(!lto)

backup=('etc/conf.d/prometheus-node-exporter')

source=("$pkgname-$pkgver.tar.gz::https://github.com/prometheus/node_exporter/archive/v$pkgver.tar.gz"
        prometheus-node-exporter.conf
        prometheus-node-exporter.service
        prometheus-node-exporter.sysusers)

sha256sums=('5da1dcc3608db663fc1949042c6f3ec29184c9414c37c0e2bdfe9b19fd5d75c7'
            'ce93e2b95bfc86a8a046e2f9175408e1cbffa784fd3b65dd141fde70b5bb2585'
            '457e305760323f941d20248e2ca5817ae4a8b5586f79a2331b968bf3baa66c5b'
            'c7fd0b1793dfe7a354a28e978d3c79e7195eaf43376b9eece37e996fe0772c5c')

build() {
  cd node_exporter-$pkgver

  go build \
    -trimpath \
    -buildmode=pie \
    -mod=readonly \
    -modcacherw \
    -ldflags "-linkmode external -extldflags ${LDFLAGS} \
      -X github.com/prometheus/common/version.Version=$pkgver \
      -X github.com/prometheus/common/version.Revision=$pkgver \
      -X github.com/prometheus/common/version.Branch=tarball \
      -X github.com/prometheus/common/version.BuildUser=someone@builder \
      -X github.com/prometheus/common/version.BuildDate=$(date --date="@${SOURCE_DATE_EPOCH:-$(date +%s)}" +%Y%m%d-%H:%M:%S)" \
    .
}

check() {
  cd node_exporter-$pkgver
  make test
  ./end-to-end-test.sh
}

package () {
  install -Dm644 prometheus-node-exporter.conf "$pkgdir"/etc/conf.d/prometheus-node-exporter
  install -Dm644 prometheus-node-exporter.service "$pkgdir"/usr/lib/systemd/system/prometheus-node-exporter.service
  install -Dm644 prometheus-node-exporter.sysusers "$pkgdir"/usr/lib/sysusers.d/prometheus-node-exporter.conf

  cd node_exporter-$pkgver

  install -Dm755 node_exporter "$pkgdir"/usr/bin/prometheus-node-exporter
}
